package algotithm.weekendrace.leetcode230.test3;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/28-10:27
 */
class Solution {

    public static void main(String[] args) {

    }

    public int minOperations(int[] n1, int[] n2) {
        int n = n1.length, m = n2.length;
        if(n > 6 * m || m > 6 * n) {
            return -1;
        }
        int sum1 = 0, sum2 = 0;
        int[] cnt1 = new int[7], cnt2 = new int[7];
        for(int i : n1) {sum1 += i; cnt1[i]++;}
        for(int i : n2) {sum2 += i; cnt2[i]++;}
        int diff = Math.abs(sum1 - sum2), res = 0;
        if(sum1 < sum2) {
            for(int i = 1; i < 6; i++) {
                if(diff <= (6 - i) * (cnt1[i] + cnt2[7 - i])) {
                    return res + (diff + 5 - i) / (6 - i);
                }
                res += (cnt1[i] + cnt2[7 - i]);
                diff -= (6 - i) * (cnt1[i] + cnt2[7 - i]);
            }
        } else {
            for(int i = 1; i < 6; i++) {
                if(diff <= (6 - i) * (cnt2[i] + cnt1[7 - i])) {
                    return res + (diff + 5 - i) / (6 - i);
                }
                res += (cnt2[i] + cnt1[7 - i]);
                diff -= (6 - i) * (cnt2[i] + cnt1[7 - i]);
            }
        }
        return res;
    }
}
